import {
  ACTION_SET_FROM,
  ACTION_SET_TO,
  ACTION_SET_DEPARTDATE,
  ACTION_SET_HIGHSPEED,
  ACTION_SET_ORDERTYPE,
  ACTION_SET_ONLYTICKETS,
  ACTION_SET_TICKETTYPES,
  ACTION_SET_CHECKEDTICKETTYPES,
  ACTION_SET_TRAINTYPES,
  ACTION_SET_CHECKEDTRAINTYPES,
  ACTION_SET_DEPARTSTATIONS,
  ACTION_SET_CHECKEDDEPARTSTATIONS,
  ACTION_SET_ARRIVESTATIONS,
  ACTION_SET_CHECKEDARRIVESTATIONS,
  ACTION_SET_DEPARTTIMESTART,
  ACTION_SET_DEPARTTIMEEND,
  ACTION_SET_ARRIVETIMESTART,
  ACTION_SET_ARRIVETIMEEND,
  ACTION_SET_ISFILTERSVISIBLE,
  ACTION_SET_SEARCHPARSED,
  ACTION_SET_TRAIN_LIST,
} from './actions';
import { ORDER_DEPART } from './constant';
function log(msg) {
  let debug = false;
  if (debug) console.log(msg);
}
export default {
  from(prevState = null, action) {
    log('reducer -> from');

    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_FROM:
        return payload;

      default:
    }
    return prevState;
  },
  to(prevState = null, action) {
    log('reducer -> to');
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_TO:
        return payload;

      default:
    }
    return prevState;
  },
  departDate(prevState = Date.now(), action) {
    log('reducer -> departDate');
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_DEPARTDATE:
        return payload;

      default:
    }
    return prevState;
  },
  //只看高铁动车
  highSpeed(prevState = false, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_HIGHSPEED:
        return payload;
      case ACTION_SET_CHECKEDTRAINTYPES: {
        const checkedTrainTypes = payload;
        return Boolean(checkedTrainTypes[1] && checkedTrainTypes[5]);
      }
      default:
    }
    return prevState;
  },
  orderType(prevState = ORDER_DEPART, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_ORDERTYPE:
        return payload;

      default:
    }
    return prevState;
  },
  onlyTickets(prevState = false, action) {
    //只看有票
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_ONLYTICKETS:
        return payload;

      default:
    }
    return prevState;
  },
  ticketTypes(prevState = [], action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_TICKETTYPES:
        return payload;

      default:
    }
    return prevState;
  },
  checkedTicketTypes(prevState = {}, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_CHECKEDTICKETTYPES:
        return payload;

      default:
    }
    return prevState;
  },
  trainTypes(prevState = [], action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_TRAINTYPES:
        return payload;

      default:
    }
    return prevState;
  },
  //设置选中的车次类型
  checkedTrainTypes(prevState = {}, action) {
    log('reducer -> checkedTrainTypes');
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_CHECKEDTRAINTYPES:
        return payload;
      case ACTION_SET_HIGHSPEED: {
        //为什么选择在这里加判断? 可能是这里的prevState对应的是checkedTrainTypes对象
        //当派发的action是highspeed 则payload
        const highSpeed = payload;
        const newCheckTrainTypes = { ...prevState };
        if (highSpeed) {
          //高铁
          newCheckTrainTypes[1] = true;
          //动车
          newCheckTrainTypes[5] = true;
        } else {
          delete newCheckTrainTypes[1];
          delete newCheckTrainTypes[5];
        }
        return newCheckTrainTypes;
      }

      default:
    }
    return prevState;
  },
  departStations(prevState = [], action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_DEPARTSTATIONS:
        return payload;

      default:
    }
    return prevState;
  },
  checkedDepartStations(prevState = {}, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_CHECKEDDEPARTSTATIONS:
        return payload;

      default:
    }
    return prevState;
  },
  arriveStations(prevState = [], action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_ARRIVESTATIONS:
        return payload;

      default:
    }
    return prevState;
  },
  checkedArriveStations(prevState = {}, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_CHECKEDARRIVESTATIONS:
        return payload;

      default:
    }
    return prevState;
  },
  departTimeStart(prevState = 0, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_DEPARTTIMESTART:
        return payload;

      default:
    }
    return prevState;
  },
  departTimeEnd(prevState = 24, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_DEPARTTIMEEND:
        return payload;

      default:
    }
    return prevState;
  },
  arriveTimeStart(prevState = 0, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_ARRIVETIMESTART:
        return payload;

      default:
    }
    return prevState;
  },
  arriveTimeEnd(prevState = 24, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_ARRIVETIMEEND:
        return payload;

      default:
    }
    return prevState;
  },
  isFiltersVisible(prevState = false, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_ISFILTERSVISIBLE:
        return payload;

      default:
    }
    return prevState;
  },
  searchParsed(prevState = false, action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_SEARCHPARSED:
        return payload;

      default:
    }
    return prevState;
  },
  trainList(state = [], action) {
    const { type, payload } = action;
    switch (type) {
      case ACTION_SET_TRAIN_LIST:
        return payload;
      default:
    }

    return state;
  },
};
